import base64
# from apis.docs import docs
from fastapi import Depends, HTTPException, status, APIRouter
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.security import OAuth2PasswordRequestForm, HTTPBasicCredentials, HTTPBasic
from starlette.status import HTTP_401_UNAUTHORIZED
from fastapi.openapi.docs import get_redoc_html

from common.base.controller import Controller, RequestGet

security = HTTPBasic()


@Controller(prefix="", tags=["接口文档"])
class DocsView:

    @RequestGet(path="/docs", include_in_schema=False)
    async def get_documentation(self, credentials: HTTPBasicCredentials = Depends(security)):
        if credentials.username != "et" or credentials.password != "et@doc@2022":
            raise HTTPException(status_code=HTTP_401_UNAUTHORIZED,
                                detail="Incorrect email or password",
                                headers={"WWW-Authenticate": "Basic"},
                                )

        return get_swagger_ui_html(
            openapi_url="/openapi.json", title="Et Admin API",
            swagger_js_url="/static/swagger-ui/swagger-ui-bundle.js",
            swagger_css_url="/static/swagger-ui/swagger-ui.css",
        )

    @RequestGet(path="/redoc", include_in_schema=False)
    async def redoc_html(self, credentials: HTTPBasicCredentials = Depends(security)):

        if credentials.username != "et" or credentials.password != "et@doc@2022":
            raise HTTPException(
                status_code=HTTP_401_UNAUTHORIZED,
                detail="Incorrect email or password",
                headers={"WWW-Authenticate": "Basic"},
            )

        return get_redoc_html(
            openapi_url="/openapi.json",
            title="Et Admin API",
            redoc_js_url="/static/redoc/bundles/redoc.standalone.js",
        )

